﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.CodeAnalysis;

namespace Beeflys.Generator
{
    public sealed class UMethodInfo
    {
        public UMethodInfo(UTypeInfo typeInfo, IMethodSymbol methodSymbol)
        {
            this.SelfType = typeInfo;
            this.methodSymbol = methodSymbol;

            
        }
        
        public void Parse()
        {
            this.Name = methodSymbol.Name;
            this.FullName = methodSymbol.ToString();
            this.IsStatic = methodSymbol.IsStatic;
            this.IsAbstract = methodSymbol.IsAbstract;
            this.IsGenericMethod = methodSymbol.IsGenericMethod;
            this.Modifiers = methodSymbol.DeclaredAccessibility;
            this.IsAsync = methodSymbol.IsAsync;
            
            this.ReturnVoid = methodSymbol.ReturnsVoid;
            this.ReturnTypeSymbol = methodSymbol.ReturnType;
            this.ParametersSymbol = methodSymbol.Parameters.ToList();
            this.GenericArguments = methodSymbol.TypeArguments.ToList();

            var items = methodSymbol.GetAttributes();
            foreach (var item in items)
            {
                this.Attributes.Add(item.AttributeClass.ToTypeInfo());
            }
        }

        public UTypeInfo SelfType { get; }
        private IMethodSymbol methodSymbol;

        public string Name { get; private set; }
        public string FullName { get; private set; }
        public bool IsStatic { get; private set; }
        public bool IsAbstract { get; private set; }
        public bool IsGenericMethod { get; private set; }

        public bool IsAsync { get; private set; }

        //修饰符
        public Accessibility Modifiers { get; private set; }
        public bool ReturnVoid { get; private set; }
        public List<ITypeSymbol> GenericArguments { get; private set; }
        private ITypeSymbol ReturnTypeSymbol { get; set; }
        private List<IParameterSymbol> ParametersSymbol { get; set; }

        public List<UTypeInfo> Attributes { get; private set; } = new List<UTypeInfo>();



        private UTypeInfo returnType = null;
        private List<UTypeInfo> parameters = new List<UTypeInfo>();
        
        public UTypeInfo GetReturnType()
        {
            if (this.ReturnVoid)
                return null;

            if (this.returnType != null)
                return this.returnType;

            this.returnType = this.ReturnTypeSymbol.ToTypeInfo();
            
            return this.returnType;
        }

        public List<UTypeInfo> GetArgsTypes()
        {
            if (this.ParametersSymbol.Count == 0)
                return this.parameters;

            if (this.parameters.Count > 0)
                return this.parameters;

            foreach (var item in this.ParametersSymbol)
            {
                this.parameters.Add(item.Type.ToTypeInfo());
            }

            return this.parameters;
        }
    }
}